package table

import (
	"fmt"
	"time"
	"yunj/app/controller/admin/demo"
	"yunj/pkg/yunj/core/builder"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/core/builder/table"
	"yunj/pkg/yunj/core/consts"

	"github.com/gin-gonic/gin"
)

type Index struct {
	demo.Controller
}

// 示例
func (ctrl *Index) Index(ctx *gin.Context) {
	tableBuilder := CommonTableBuilder(ctx, "desc")
	tableBuilder.Cols(func(t *builder.Table, state *builderdto.TableState) (cols []table.ColInterface, err error) {
		cols, err = CommonCols(ctx, state, false)
		return
	}).Count(func(t *builder.Table, requestParam *builderdto.TableRequestParam) (res builderdto.TableCountResult, err error) {
		res.Count = 4
		return
	}).Items(func(t *builder.Table, requestParam *builderdto.TableRequestParam) (res builderdto.TableItemsResult, err error) {
		items := []map[string]any{}
		for i := 0; i < 4; i++ {
			items = append(items, map[string]any{
				"id":               fmt.Sprintf("%v_%d", requestParam.State, i+1),
				"name":             fmt.Sprintf("小王_%d", i+1),
				"age":              18 + i,
				"grade":            (i + 1) % 5,
				"created_at":       time.Now().Format("2006-01-02 15:04:05"),
				"avatar":           consts.IMG_URI,
				"album":            []string{consts.IMG_URI, consts.IMG_URI},
				"personal_profile": consts.TEST_FILE_URL,
				"other_profile":    []string{consts.TEST_FILE_URL, consts.TEST_FILE_URL},
				"color":            "#fff000",
				"icon":             "yunj-icon-click",
			})
		}
		res.Items = items
		return
	})
	ctrl.RenderTable(ctx, tableBuilder)
}

// 示例
func (ctrl *Index) Tree(ctx *gin.Context) {
	tableBuilder := CommonTableBuilder(ctx, "desc")
	tableBuilder.Tree(func(t *builder.Table, state *builderdto.TableState) (bool, error) {
		return true, nil
	}).Cols(func(t *builder.Table, state *builderdto.TableState) (cols []table.ColInterface, err error) {
		cols, err = CommonCols(ctx, state, true)
		return
	}).Count(func(t *builder.Table, requestParam *builderdto.TableRequestParam) (res builderdto.TableCountResult, err error) {
		res.Count = 4
		return
	}).Items(func(t *builder.Table, requestParam *builderdto.TableRequestParam) (res builderdto.TableItemsResult, err error) {
		items := []map[string]any{}
		for i := 0; i < 4; i++ {
			items = append(items, map[string]any{
				"id":               fmt.Sprintf("%v_%d", requestParam.State, i+1),
				"pid":              "",
				"name":             fmt.Sprintf("小王_%d", i+1),
				"age":              18 + i,
				"grade":            (i + 1) % 5,
				"created_at":       time.Now().Format("2006-01-02 15:04:05"),
				"avatar":           consts.IMG_URI,
				"album":            []string{consts.IMG_URI, consts.IMG_URI},
				"personal_profile": consts.TEST_FILE_URL,
				"other_profile":    []string{consts.TEST_FILE_URL, consts.TEST_FILE_URL},
				"color":            "#fff000",
				"icon":             "yunj-icon-click",
			})
		}
		items[2]["pid"] = fmt.Sprintf("%v_%d", requestParam.State, 2)
		items[3]["pid"] = fmt.Sprintf("%v_%d", requestParam.State, 2)
		res.Items = items
		return
	})
	ctrl.RenderTable(ctx, tableBuilder)
}
